package mlo.mattetal.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Mattetal implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		final Button sendButton = new Button("Generera");
		
		final TextBox rowsField = new TextBox();
		rowsField.setText("21");
		rowsField.setMaxLength(2);
		
		
		final Label errorLabel = new Label();
		// final Label resultLabel= new Label();
		final FlexTable resultTable = new FlexTable  ();
		resultTable.setStyleName("panel flexTable");
		
		// We can add style names to widgets
		sendButton.addStyleName("sendButton");

		// Add the nameField and sendButton to the RootPanel
		// Use RootPanel.get() to get the entire body element
		RootPanel.get("rowFieldContainer").add(rowsField);
		RootPanel.get("genButtonContainer").add(sendButton);
		RootPanel.get("errorLabelContainer").add(errorLabel);
		RootPanel.get("resultContainer").add(resultTable);
		// Focus the cursor on the name field when the app loads


		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				try {
					errorLabel.setText("");
					int rows=Integer.valueOf(rowsField.getText());
					resultTable.removeAllRows();
					int r=0;
					for (int row = 0; row < rows; row++) {
						  r = row;
					      for (int col = 0; col < 5; ++col) 
					      {
					    	  int item = row + col*rows + 1;

					    	  String text = item + ".    " +  Random.nextInt(10) + " * " + 
					    	                Random.nextInt(10) + " = ______";
					    	  Label lbl = new Label (text);
					    	  lbl.getElement().getStyle().setFontSize(18, Unit.PX);
					    	  lbl.setWidth("180px");
					    	  lbl.setHeight("50px");
					    	  resultTable.setWidget(r, col, lbl);
					      }
					      
					      
					}
				} catch (NumberFormatException ex) {
					errorLabel.setText("Bara siffror tillåtna");
				}
			}
			
			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {

			}

		}

		// Add a handler to send the name to the server
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
	}
}
